<template>
	<div class="sys-dictType-container">
		<el-dialog v-model="state.isShowDialog" draggable :close-on-click-modal="false" width="700px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit />
					</el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="orderFormRef" :rules="rules" :dispose="true" label-width="120px">
				<el-form-item label="出入库单号" prop="orderId">
					<el-input v-model="state.ruleForm.orderId" placeholder="出入库单号不填自己生成" :disabled="true" />
				</el-form-item>
				<el-form-item label="托盘条码" prop="tray">
					<el-input v-model="state.ruleForm.tray" placeholder="请输入托盘条码" />
				</el-form-item>
				<el-form-item label="出入口" prop="hoistOpening">
					<el-select v-model="state.ruleForm.hoistOpening" placeholder="请选择出入口" @change="ChangeType">
						<el-option v-for="[value, label] in Object.entries(hoistOpeningList)" :key="value" :label="label"
							:value="value" />
					</el-select>
				</el-form-item>
				<!-- <el-form-item label="订单类型" prop="orderType">
					<el-select v-model="state.ruleForm.orderType" placeholder="请选择出入库订单类型" @change="ChangeType">
						<el-option v-for="[value, label] in Object.entries(orderTypeMap)" :key="value" :label="label"
							:value="Number(value)" />
						<el-option v-if="!orderTypeMap[state.ruleForm.orderType]"
							:label="'未知类型 (' + state.ruleForm.orderType + ')'" :value="state.ruleForm.orderType" />
					</el-select>
				</el-form-item>

				<el-form-item label="质检状态" prop="quality" :disable="isEdit">
					<el-select v-model="state.ruleForm.quality" placeholder="请选择质检状态">
						<el-option v-for="[value, label] in Object.entries(qualityMap)" :key="value" :label="label"
							:value="Number(value)" />

					</el-select>
				</el-form-item>


				<el-form-item label="订单当前状态" prop="orderStatus">
					<el-select v-model="state.ruleForm.orderStatus" placeholder="请选择订单状态">
						<el-option v-for="[value, label] in Object.entries(orderStatusMap)" :key="value" :label="label"
							:value="Number(value)" />
						<el-option v-if="!Object.keys(orderStatusMap).includes(state.ruleForm.orderStatus.toString())"
							:label="`未知状态 (${state.ruleForm.orderStatus})`" :value="state.ruleForm.orderStatus" />
					</el-select>
				</el-form-item>


				<el-form-item label="物料编码" prop="materialCode">
					<el-input v-model="state.ruleForm.materialCode" placeholder="请输入物料编码" />
				</el-form-item>
				<el-form-item label="物料名称" prop="materialName">
					<el-input v-model="state.ruleForm.materialName" placeholder="请输入物料名称" />
				</el-form-item>
				<el-form-item label="箱条码" prop="boxCode">
					<el-input v-model="state.ruleForm.boxCode" placeholder="请输入箱条码" />
				</el-form-item>
				<el-form-item label="出-数量" prop="number">
					<el-input v-model="state.ruleForm.number" type="number" placeholder="请输入出-数量" :number="true" />
				</el-form-item>
				<el-form-item label="出-波次号" prop="wave">
					<el-input v-model="state.ruleForm.wave" placeholder="请输入出-波次号" />
				</el-form-item>
				<el-form-item label="入-批次" prop="batch">
					<el-input v-model="state.ruleForm.batch" placeholder="请输入入-批次" />
				</el-form-item>
				<el-form-item label="入-参考号" prop="reference">
					<el-input v-model="state.ruleForm.reference" placeholder="请输入入-参考号" />
				</el-form-item>
				<el-form-item label="出入库时间" prop="outwarehosueDate">
					<el-date-picker v-model="state.ruleForm.outwarehosueDate" type="datetime" placeholder="选择出入库时间" />
				</el-form-item> -->
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取消</el-button>
					<el-button type="primary" @click="submit">确定</el-button>
				</span>
			</template>
		</el-dialog>



	</div>
</template>

<script lang="ts" setup name="EditOrder">
const hoistOpeningList = {
	'出入口1': '出入口1',
	'出入口2': '出入口2',
	'出入口3': '出入口3',
	'出入口4': '出入口4',
	'出入口5': '出入口5'
};
const orderTypeMap = {
	0: '其他',
	1: '领料出库',
	2: '计划外出库',
	3: '计划手工出库',
	4: '采购收货入库',
	5: '产线退货入库',
	6: '手工入库'
};
const orderTypeorderMap = {
	0: 'Q',
	1: 'CK',
	2: 'CK',
	3: 'CK',
	4: 'RK',
	5: 'RK',
	6: 'RK'
}

const qualityMap = {
	0: '隔离',
	1: '正常',
	2: '待检'
};
const orderStatusMap = { 0: '未开始', 1: '进行中', 2: '退回', 3: '完成', 4: '取消', 5: '挂账' };
import { onMounted, reactive, ref } from 'vue';
import { dayjs, ElMessage, ElMessageBox } from 'element-plus';
import { getAPI } from '/@/utils/axios-utils';

import { AccountTypeEnum, UpdateDictTypeInput } from '/@/api-services/models';
import { useUserInfo } from '/@/stores/userInfo';
import { OrderinfoApi } from '/@/api-services/apis/warehouse-orderinfo-api';; // 导入 API 服务
import { DeleteOrderinfoInput, OrderInfo, OrderStatusEnum } from '/@/api-services/models/order-input'; // 导入获取实体
import { tr } from 'element-plus/es/locale';
import { dispose } from 'echarts';

const isEdit = ref(false);
const orderFormRef = ref();

const state = reactive({
	loading: false,
	isShowDialog: false,
	ruleForm: {} as OrderInfo,
});

const orderForm = reactive({
	id: 0,
	orderId: '',
	orderType: '',
	quality: '',
	orderStatus: '',
	materialCode: '',
	materialName: '',
	boxCode: '',
	trayCode: '',
	number: 0,
	wave: '',
	batch: '',
	reference: '',
	outwarehosueDate: null
});
const rules = {
	orderId: [{ required: true, message: '出入库订单号不能为空', trigger: 'blur' }],
	orderType: [{ required: true, message: '出入库订单类型不能为空', trigger: 'change' }],
	quality: [{ required: true, message: '质检状态不能为空', trigger: 'change' }],
	orderStatus: [{ required: true, message: '订单状态不能为空', trigger: 'change' }],
	materialCode: [{ required: true, message: '物料编码不能为空', trigger: 'blur' }],
	materialName: [{ required: true, message: '物料名称不能为空', trigger: 'blur' }],
	boxCode: [{ required: true, message: '箱条码不能为空', trigger: 'blur' }],
	trayCode: [{ required: true, message: '托盘条码不能为空', trigger: 'blur' }],
	number: [{ required: true, message: '出-数量不能为空', trigger: 'blur' }],
	wave: [{ required: true, message: '出-波次号不能为空', trigger: 'blur' }],
	batch: [{ required: true, message: '入-批次不能为空', trigger: 'blur' }],
	reference: [{ required: true, message: '入-参考号不能为空', trigger: 'blur' }],
	outwarehosueDate: [{ required: true, message: '出入库时间不能为空', trigger: 'change' }]
};


const openDialog = (row: any) => {
	state.ruleForm = JSON.parse(JSON.stringify(row));
	state.isShowDialog = true;
	orderFormRef.value?.resetFields();
};


const props = defineProps({
	title: String,
});
const emits = defineEmits(['handleQuery']);


// 关闭弹窗
const closeDialog = () => {
	emits('handleQuery');
	state.isShowDialog = false;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 有序 ID，初始值为 1
const sequenceId = ref(1);
// 存储生成的订单号
const orderNumber = ref('');

const ChangeType = () => {
	// 生成一个 0 到 100 之间的随机整数
	const randomNumber = Math.floor(Math.random() * 101);
	console.log('生成的随机数是:', randomNumber);
	// 存储页面首次加载时的 sequenceId


	// 获取订单类型对应的首字母
	const orderTypeValue = state.ruleForm.orderType;
	const orderTypeLabel = orderTypeorderMap[orderTypeValue] || 'MOVE';
	const orderTypeInitial = orderTypeLabel.toUpperCase();	
	// 获取当前时间，格式为 YYYYMMDD
	const now = new Date();
	const year = now.getFullYear();
	const month = String(now.getMonth() + 1).padStart(2, '0');
	const day = String(now.getDate()).padStart(2, '0');
	const minutes = String(now.getMinutes()).padStart(2, '0');
	const datePart = `${year}${month}${day}${minutes}`;
	// 生成有序 IDs
	const currentSequenceId = sequenceId.value

	// 组合成订单号 或者从后端获取表里最大的id 然后+1
	const generatedOrderNumber = `${orderTypeInitial}${datePart}${randomNumber}`;
	// 将生成的订单号赋值给 state.ruleForm.orderId
	state.ruleForm.orderId = generatedOrderNumber;
	return generatedOrderNumber;

};
// const hours = String(now.getHours()).padStart(2, '0');
// const minutes = String(now.getMinutes()).padStart(2, '0');
// const seconds = String(now.getSeconds()).padStart(2, '0');
//const timestamp = `${year}${month}${day}${hours}${minutes}${seconds}`;
// 生成有序 ID

// 组合成订单号



// 提交
const submit = () => {

	if (!orderFormRef.value) return;
	orderFormRef.value.validate(async (valid: boolean) => {

		if (!valid) return;
		if (state.ruleForm.id !== undefined && state.ruleForm.id > 0) {

			await getAPI(OrderinfoApi).apiOrderinfoUpdatePost(state.ruleForm);
			ElMessage.success('编辑成功');
		} else {

			await getAPI(OrderinfoApi).apiOrderinfoAddPost(state.ruleForm);
		}
		closeDialog();
	});

};

// 导出对象
defineExpose({ openDialog });
</script>
